package com.bestcem.xm.txtanls.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.txtanls.controller.convert.TrainModelConvert;
import com.bestcem.xm.txtanls.controller.vo.model.TrainModelVO;
import com.bestcem.xm.txtanls.dao.TrainModelDao;
import com.bestcem.xm.txtanls.entity.mysql.TrainModelDo;
import com.bestcem.xm.txtanls.grpc.client.BaseUserGrpcService;
import com.bestcem.xm.txtanls.service.TrainModelService;
import com.bestcem.xm.txtanls.service.dto.user.IndustryDTO;
import com.bestcem.xm.txtanls.service.dto.user.OrgInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author liheng
 * @version v1.0
 * @date 2022/3/9 15:30
 */

@Slf4j
@Service
public class TrainModelServiceImpl implements TrainModelService {

    @Resource
    private TrainModelDao trainModelDao;

    @Resource
    private BaseUserGrpcService baseUserGrpcService;

    @Override
    public ServiceResult<List<TrainModelVO>> listTrainModel() {
        List<TrainModelDo> trainModels = trainModelDao.listTrainModel();
        List<TrainModelVO> trainModelVOS = TrainModelConvert.convertVoList(trainModels);
        return ServiceResult.success(trainModelVOS);
    }

    @Override
    public String getTrainModelId(String orgId) {
        OrgInfoDTO organization = baseUserGrpcService.getOrganization(orgId);
        if (Objects.isNull(organization)) {
            return "";
        }
        String industryId = organization.getIndustryID();
        if (!ObjectId.isValid(industryId)) {
            return "";
        }
        IndustryDTO industry = baseUserGrpcService.getIndustry(industryId);
        if (Objects.isNull(industry)) {
            return "";
        }
        String industryCode = industry.getCode();
        return this.getDefaultModelId(Integer.valueOf(industryCode));

    }

    @Override
    public String getDefaultModelId(Integer code) {
        TrainModelDo trainModel = trainModelDao.getByCode(code);
        if (Objects.nonNull(trainModel)) {
            return trainModel.getTrainModelId();
        }
        return "";
    }


}
